﻿
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using GDFlacTool.Common;

namespace GDFlacTool.forms
{
    public partial class FormDiscNumber : Form
    {
        private readonly IList<Track> _tracks;

        public FormDiscNumber(IList<Track> tracks)
        {
            InitializeComponent();

            _tracks = tracks;

            chkFormat.Checked = Properties.Settings.Default.UseDiscFormat;

            chkSecondFormat.Checked = Properties.Settings.Default.UseSecondDiscFormat;

            nudOfDisc.Value = Properties.Settings.Default.DiscOfDiscValue;

            nudDisc.Value = Properties.Settings.Default.DiscValue;

            PreviewDiscNumber();
        }

        private void chkFormat_CheckedChanged(object sender, EventArgs e)
        {
            if(chkFormat.Checked)
            {
                nudOfDisc.Enabled = true;
            }
            else
            {
                nudOfDisc.Enabled = false;
            }

            PreviewDiscNumber();
        }

        private void chkSecondFormat_CheckedChanged(object sender, EventArgs e)
        {
            PreviewDiscNumber();
        }

        private void btnFillDiscNumber_Click(object sender, EventArgs e)
        {
            foreach (Track track in _tracks)
            {
                if(chkFormat.Checked)
                {
                    if(chkSecondFormat.Checked)
                    {
                        track.DiscNumber = string.Format("{0:0#}/{1:0#}", nudDisc.Value, nudOfDisc.Value);
                    }
                    else
                    {
                        track.DiscNumber = string.Format("{0}/{1}", nudDisc.Value, nudOfDisc.Value);
                    }
                }
                else
                {
                    if(chkSecondFormat.Checked)
                    {
                        track.DiscNumber = string.Format("{0:0#}", nudDisc.Value);
                    }
                    else
                    {
                        track.DiscNumber = string.Format("{0}", nudDisc.Value);
                    }
                }
            }
        }

        private void PreviewDiscNumber()
        {
            string preview;

            if (chkFormat.Checked)
            {
                if (chkSecondFormat.Checked)
                {
                    preview = string.Format("{0:0#}/{1:0#}", nudDisc.Value, nudOfDisc.Value);
                }
                else
                {
                    preview = string.Format("{0}/{1}", nudDisc.Value, nudOfDisc.Value);
                }
            }
            else
            {
                if (chkSecondFormat.Checked)
                {
                    preview = string.Format("{0:0#}", nudDisc.Value);
                }
                else
                {
                    preview = string.Format("{0}", nudDisc.Value);
                }
            }

            lblInfo.Text = string.Format("Auto fill disc number for all selected files, example ({0})", preview);
        }

        private void FormDiscNumber_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.UseDiscFormat = chkFormat.Checked;

            Properties.Settings.Default.UseSecondDiscFormat = chkSecondFormat.Checked;

            Properties.Settings.Default.DiscOfDiscValue = nudOfDisc.Value;

            Properties.Settings.Default.DiscValue = nudDisc.Value;

            Properties.Settings.Default.Save();
        }

        
    }
}
